# coding=utf-8
# al训练脚本
# 读入al训练信息，进行训练操作，保存更新后的al训练信息

import sys
import os

import json

from rs_label.active_learning.active_learning import ActiveLearning
from rs_label.active_learning import al_train_info
from rs_label.util.parameter import Para
from rs_label.tools.db.db_conn import Connection

def ALTrainFromController(al_train_info_json, configFileName):
    al_train_info_instance = al_train_info.AlTrainInfo(al_train_info_json)
    para = Para(configFileName)
    conn = Connection(para.db_type)
    conn.open_conn(db_path=para.db_name)

    al = ActiveLearning()
    # AL训练过程后，更新训练信息
    al_train_info_instance_refresh = al.train_process(al_train_info_instance, conn)
    # 将ALTrainInfo转为json字符串
    al_train_info_json_refresh = al_train_info_instance_refresh.to_json()
    return al_train_info_json_refresh

"""
# 参数为train_info所在文件地址
# 文件在tmp文件夹下，格式为 "%s_train_info_info.json" % rs_name
# 其中rs_name为遥感图像名称
al_train_info_name = sys.argv[1]
with open(al_train_info_name, "r") as f:
    al_train_info_json = reduce(lambda s1, s2: s1+s2, f.readlines())
# 将json转为AlTrainInfo
al_train_info_instance = al_train_info.AlTrainInfo(al_train_info_json)

# config file path
config_file_name = sys.argv[2]
para = Para(config_file_name)

conn = Connection(para.db_type)
conn.open_conn(db_path=para.db_name)

al = ActiveLearning()
# AL训练过程后，更新训练信息
al_train_info_instance_refresh = al.train_process(al_train_info_instance, conn)
# 将ALTrainInfo转为json字符串
al_train_info_json_refresh = al_train_info_instance_refresh.to_json()
# 将json字符串保存到指定的位置
# 文件在tmp文件夹下，格式为 "%s_train_info" % rs_name
# 其中rs_name为遥感图像名称
# 将覆盖原来的训练信息
al_train_info_name_refresh = os.path.join(para.tmp, "%s_train_info.json" % al_train_info_instance.rs_name)
with open(al_train_info_name_refresh, "w") as f:
    f.write(al_train_info_json_refresh)
"""